package com.gr.staffpm.projects.service;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.projects.model.DetachableProjectModel;


public class SortableProjectDataProvider extends SortableDataProvider<Project> {

    private final ProjectService projectService;
    private static final long serialVersionUID = 1L;

    public SortableProjectDataProvider(ProjectService projectService) {
        //Set default sort
        setSort(Project.NAME, true);
        this.projectService = projectService;
    }

    /**
     * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
     */
    @Override
    public Iterator<? extends Project> iterator(int first, int count) {
        SortParam sp = getSort();
        return projectService.getAllProjects(first, count, sp.getProperty(), sp.isAscending()).iterator();
    }

    @Override
    public IModel<Project> model(Project project) {
        return new DetachableProjectModel(projectService, project);
    }

    @Override
    public int size() {
        return projectService.getAllProjects().size();
    }

}
